<template>
	<view class="detail">
		<head-nav :title="songDetail.name" :showMenu="true" color="white"></head-nav>
		<loading v-if="!pagestatus"></loading>
		<template v-else>
			<view class="cover" :style="{top:stb+'px'}">
				<image :src="songDetail.al.picUrl" v-if="songDetail.al.picUrl"></image>
				<view style="background: black;width: 100%;height: 100%;" v-else></view>
			</view>
			<container>
				<!-- 此处有插槽，可以直接把组件当父标签 -->

				<view class="playerBox">
					<view class="zhen">
						<image src="../../static/zhen.png"></image>
					</view>
					<view class="circle" :class="status?'':'stop'">
						<image :src="songDetail.al.picUrl"></image>
					</view>
					<view class="control" @tap='playcontrol()'>
						<text :class="status?'icon-zantingtingzhi':'icon-yousanjiao'" class="iconfont"></text>
					</view>
				</view>

				<view class="lyricsBox">
					<view id="lyricsWrap" :style="{transform:'translateY('+-11*(curLyc-1)+'vw)'}" v-if="lyric.length">
						<view v-for="(n,i) in lyric" :key="i">
							<text :class="{active:curLyc==i?true:false}">{{n.lyc}}</text>
						</view>
					</view>
					<view v-else class="noLyc">抱歉，暂无歌词提供</view>
				</view>

				<view class="otherLike">
					喜欢这首歌的人也听
				</view>

				<view class="likeBox">
					<view class="each" v-for="(n,i) in simiSong" :key='i'>
						<view class="left">
							<image :src="n.album.picUrl"></image>
						</view>
						<view class="mid">
							<view class="up">
								<text>{{n.name}}</text>
							</view>
							<view class="down">
								<image src="../../static/vip.png"></image>
								<text>{{n.artists[0].name}}{{n.artists[1]?' / '+n.artists[1].name:''}}-{{n.name}}</text>
							</view>
						</view>
						<view class="right" @tap='toOther(n.id)'>
							<text class="iconfont icon-bofang1"></text>
						</view>
					</view>
				</view>

				<view class="comment">
					<view class="top">
						精彩评论
					</view>
					<view class="comBox" v-for="(n,i) in comment" :key='i'>
						<view class="each">
							<view class="left">
								<image :src="n.user.avatarUrl"></image>
							</view>
							<view class="mid">
								<view class="up">
									<text>{{n.user.nickname}}</text>
								</view>
								<view class="down">
									<text>{{n.time | time}}</text>
								</view>
							</view>
							<view class="right">
								{{n.likedCount}}<text class="iconfont icon-zan"></text>
							</view>
						</view>
						<view class="words">
							<text>
								<text v-if="n.beReplied[0]" class="beReplied">回复@{{n.beReplied[0].user.nickname}}:</text>
								{{n.content}}
								<text class="beReplied-words" v-if="n.beReplied[0]">
									@{{n.beReplied[0].user.nickname}}
									{{n.beReplied[0].content}}
								</text>
							</text>
						</view>
					</view>
					<view style="color: lightgray;text-align: center;" v-if="!comment.length">暂无精彩评论</view>
					<view class="endsafe"></view>
				</view>


			</container>
		</template>



	</view>
</template>

<script>
	import headNav from '../../components/headNav/headNav.vue'
	import container from '../../components/container/container.vue'
	import {
		songDetail,
		simiSong,
		comment,
		lyric,
		songUrl
	} from '../../common/api.js'
	export default {
		data() {
			return {
				songDetail: [],
				simiSong: [],
				comment: [],
				stb: '',
				lyric: [],
				curLyc: 0,
				status: true,
				pagestatus: false,
				id: ''
			};
		},
		onShow() {
			this.init(this.id)
		},
		onLoad({ //单页面和全局app页面  的生命周期和vue不一样  但是组件一样
			id
		}) {
			this.id = id
			this.init(id)
			this.stb = uni.getSystemInfoSync().statusBarHeight
		},
		methods: {
			toOther(id) {
				this.pagestatus = false
				this.bgAudioManager.pause()
				this.init(id)
			},
			init(id) {
				//可添加loading层

				//this.$store.commit('setNextID', id) 舍弃自动切歌			
				//后期用vuex给父组件传值

				Promise.all([songDetail(id), simiSong(id), comment(id), lyric(id), songUrl(id)]).then(res => {
					this.pagestatus = true
					console.log(res[2].data)
					this.songDetail = res[0].data.songs[0]
					this.simiSong = res[1].data.songs
					this.comment = res[2].data.hotComments
					if (res[3].data.lrc) {
						let lyric = res[3].data.lrc.lyric

						//正则的逻辑是 time是寻找]结尾的  lyc是寻找[开头的
						const reg = /\[([^\]]+)\]([^\[]+)/g
						let rs = []
						lyric.replace(reg, ($0, $1, $2) => {
							rs.push({
								time: this.toSeconds($1),
								lyc: this.emptyParse($2)
							})
						})
						this.lyric = rs
					} else {
						this.lyric = []
					}

					this.songUrl = res[4].data.data[0].url

					// #ifdef MP-WEIXIN
					const bgAudioManager = uni.getBackgroundAudioManager();
					bgAudioManager.title = this.songDetail.name;
					bgAudioManager.singer = '蔡徐坤';
					bgAudioManager.coverImgUrl = this.songDetail.al.picUrl
					bgAudioManager.src = this.songUrl
					// #endif

					// #ifdef H5
					const bgAudioManager = this.$store.state.h5audio ? this.$store.state.h5audio : uni.createInnerAudioContext() //多例，需要全局管理
					bgAudioManager.src = this.songUrl
					bgAudioManager.autoplay = true
					bgAudioManager.sessionCategory = 'soloAmbient' //中止其他声音播放，不能后台播放，静音后无声音； 
					// #endif


					//让播放器和页面互通状态
					this.watchLyc()
					bgAudioManager.onPlay(() => {
						this.watchLyc()
						this.status = true
					})
					bgAudioManager.onPause(() => {
						clearInterval(this.timer)
						this.status = false
					})
					bgAudioManager.onEnded(() => {
						clearInterval(this.timer)
						this.status = false
					})
					this.bgAudioManager = bgAudioManager
					this.$store.commit('seth5audio', bgAudioManager)
				})
			},

			toSeconds(e) {
				return e.split(':')[0] * 60 + e.split(':')[1] * 1
			},
			emptyParse(e) {
				//网易云回车符我是搞不懂
				return e.length == 1 ? '---' : e
			},
			playcontrol() {
				//页面与播放器互通
				this.status = !this.status
				this.status ? this.bgAudioManager.play() : this.bgAudioManager.pause()
			},
			watchLyc() {
				clearInterval(this.timer)
				let arr = this.lyric
				//无歌词不跑词
				if (arr.length > 0) {
					this.timer = setInterval(() => {
						//这里不适合用find
						for (var i = 0; i < arr.length; i++) {
							if (Math.abs(arr[i].time - this.bgAudioManager.currentTime) <= 0.5) {
								if (this.curLyc == i) {
									continue //适配快歌
								} else {
									this.curLyc = i
								}
							}
						}
					}, 200) //按照1秒5个字速度计算到200毫秒(rapper水平) 
				}

			}
		},
		mounted() {

			/*
			uni-app  未优化的问题
			无法获取 循环+动态 元素 
			 解决：
			 1.settimeout里取到
			 2.this.$nextTick
			*/

			// const query = uni.createSelectorQuery().in(this);
			// let _this = this;
			// query
			// 	.selectAll('.comment .comBox  ')
			// 	.boundingClientRect(data => {
			// 		console.log('5555',data)
			// 		//getApp().globalData	获取全局变量				
			// 	})
			// 	.exec();
		},
		components: {
			headNav,
			container
		}
	}
</script>

<style lang="scss" scoped>
	@keyframes move {
		from {
			transform: rotate(0);
		}

		to {
			transform: rotate(360deg);
		}
	}

	.endsafe {
		height: 50rpx;
	}

	.cover {
		position: fixed;
		width: 100%;
		height: 100%;
		overflow: hidden;
		background: black;

		image {
			width: 100%;
			height: 100%;
			transform: scale(1.2);
			filter: blur(2px) brightness(0.3);
		}
	}

	.playerBox {
		width: 750rpx;
		height: 750rpx;
		position: relative;
		background-size: 100% 100%;

		.zhen {
			transform: rotate(-20deg) translateX(50rpx);
			left: calc(50% - 100rpx);
			position: absolute;
			width: 200rpx;
			height: 500rpx;
			z-index: 2;

			image {
				width: 100%;
				height: 100%;
			}
		}

		.circle {
			box-sizing: unset;
			position: absolute;
			width: 330rpx;
			height: 330rpx;
			border-radius: 50%;
			border: black 80rpx solid;
			left: calc(50% - 245rpx);
			bottom: 0;
			overflow: hidden;
			box-shadow: 0 0 8px 1px #795548;
			animation: move 10s linear infinite;


			image {
				position: absolute;
				width: 100%;
				height: 100%;
				border-radius: 50%;
				transform: scale(1.1);
			}




		}

		.stop {
			animation-play-state: paused;
		}

		.control {
			z-index: 1;
			position: absolute;
			bottom: 184rpx;
			left: calc(50% - 61rpx);
			width: 120rpx;
			height: 120rpx;
			background: rgba($color: #000000, $alpha: 0.3);
			border-radius: 50%;
			border: 1px white solid;
			text-align: center;
			line-height: 120rpx;

			text {
				color: white;
				font-size: 60rpx;
			}

		}
	}

	.lyricsBox {
		margin: 50rpx auto 0;
		width: 700rpx;
		height: 33vw;
		overflow: hidden;

		.noLyc {
			color: white;
			height: 100%;
			text-align: center;
			line-height: 33vw;
		}

		#lyricsWrap {

			transition: all 0.5s linear;

			view {
				overflow: hidden;
				display: flex;
				height: 11vw; //注意  歌词这一块需要用vw来适配安卓手机   rpx转化在安卓上有误差	
				justify-content: center;
				align-items: center;

				text {
					font-size: 26rpx;
					color: #6f6e73;
					text-overflow: ellipsis;
					overflow: hidden;
					white-space: nowrap;
				}
			}

			.active {
				font-size: 30rpx;
				font-weight: bolder;
				color: rgba(255, 0, 0, .8);
			}
		}
	}

	.otherLike {
		height: 50rpx;
		width: 90%;
		margin: 40rpx auto;
		line-height: 50rpx;
		color: white;
		font-size: 35rpx;
	}

	.likeBox {
		.each {
			margin: 25rpx auto;
			width: 90%;
			height: 80rpx;
			display: flex;

			.left {

				image {
					width: 100%;
					height: 100%;
				}

				height: 80rpx;
				text-align: center;
				width: 80rpx;
				font-size: 35rpx;
				color: lightgray;
				margin-right: 25rpx;
				border-radius: 15rpx;
				overflow: hidden;
			}

			.mid {
				width: 75%;
				display: flex;
				flex-direction: column;
				justify-content: space-between;

				.up {
					position: relative;
					font-size: 28rpx;
					color: white;

					text {
						color: white;
						display: block;
						width: 80%;
						overflow: hidden;
						white-space: nowrap;
						text-overflow: ellipsis;
					}
				}

				.down {
					display: flex;
					align-items: center;

					image {
						width: 50rpx;
						height: 25rpx;
						margin-right: 10rpx;
					}

					font-size: 25rpx;
					color: rgba($color: #c2c2c2, $alpha: 1);

					text {
						display: block;
						width: 80%;
						overflow: hidden;
						white-space: nowrap;
						text-overflow: ellipsis;
					}
				}
			}

			.right {
				width: 10%;
				display: flex;
				align-items: center;
				justify-content: flex-end;

				text {
					font-size: 50rpx;
					font-weight: 100;
					color: rgba($color: #c2c2c2, $alpha: 0.7);
				}
			}

		}
	}


	.comment {
		margin: 60rpx auto 0;
		width: 90%;

		.top {
			height: 50rpx;
			width: 90%;
			line-height: 50rpx;
			color: white;
			font-size: 35rpx;
			margin-bottom: 40rpx;
		}

		.comBox {
			.each {
				margin: 25rpx auto;
				width: 100%;
				height: 65rpx;
				display: flex;

				.left {

					image {
						width: 100%;
						height: 100%;
						border-radius: 50%;
					}

					height: 65rpx;
					text-align: center;
					width: 65rpx;
					font-size: 35rpx;
					color: lightgray;
					margin-right: 25rpx;
					border-radius: 15rpx;
				}

				.mid {
					width: 65%;
					display: flex;
					flex-direction: column;
					justify-content: space-between;

					.up {
						position: relative;
						font-size: 28rpx;

						text {
							color: rgba($color: #c5c5c5, $alpha: 1.0);
							display: block;
							width: 80%;
							overflow: hidden;
							white-space: nowrap;
							text-overflow: ellipsis;
						}
					}

					.down {
						display: flex;
						align-items: center;

						image {
							width: 50rpx;
							height: 25rpx;
							margin-right: 10rpx;
						}

						font-size: 18rpx;
						color: rgba($color: #acacac, $alpha: 1);

						text {
							display: block;
							width: 80%;
							overflow: hidden;
							white-space: nowrap;
							text-overflow: ellipsis;
						}
					}
				}

				.right {
					color: rgba($color: #9e9e9e, $alpha: 1.0);
					flex: 1;
					width: 10%;
					display: flex;
					align-items: center;
					justify-content: flex-end;
					font-size: 30rpx;

					text {
						font-weight: 100;
						color: rgba($color: #c2c2c2, $alpha: 0.7);
					}
				}

			}

			.words {
				width: calc(100% - 90rpx);
				margin-left: 90rpx;
				border-bottom: 2rpx solid rgba($color: #585858, $alpha: 1.0);
				padding-bottom: 20rpx;

				text {
					color: white;
					font-size: 28rpx;
					line-height: 40rpx;
					/*
					 全局函数  在uni.scss文件
					 要填整数，不然css异常  这里是700/40取整 17行
					 
					 网易云评论区有字数限制,所以这里不用做超出优化也行
					 @include moreLineOver(17);
					*/
				}

				.beReplied {
					color: red;
				}

				.beReplied-words {
					display: block;
					margin: 10rpx 0;
					background: rgba($color: #a2a2a2, $alpha: 0.4);
					border-radius: 10rpx;
					border: 1px solid rgba($color: #7a7a7a, $alpha: 0.6);
					padding: 10rpx;
				}
			}
		}

	}
</style>
